package astri.pdcontrol;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import astri.pdcontrol.client.PDControlClient;
import astri.pdcontrol.controlmode.CtrlModeActivity;

public class PDControlActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Solve the problem of PNG image distortion
		this.getWindow().setFormat(PixelFormat.RGBA_8888);
		System.setProperty("java.net.preferIPv6Addresses", "false");

		Button mButton1 = (Button) findViewById(R.id.Button01);
		mButton1.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText et_ip = (EditText) findViewById(R.id.edit_text_ip);
				String server_ip = et_ip.getText().toString();

				EditText et_port = (EditText) findViewById(R.id.edit_text_port);
				int server_port = Integer
						.parseInt(et_port.getText().toString());

				//EditText et_pwd = (EditText) findViewById(R.id.edit_text_pwd);
				//String server_pwd = et_pwd.getText().toString();

				//testApp a = new testApp();
				//PDControlClient client = new PDControlClient(3000, 3000,a.new MyListener());
				
				ConnectApplication app = (ConnectApplication)getApplication();
				
				if (app.setClient(server_ip, server_port) == false) {
					Log.v("login", "connect fail!");
				} else {
					PDControlClient client = app.getclient();
					try {
						client.sendQtKey(0x010000a7);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.v("login", "connect success!");
					
					Intent intent = new Intent();
					intent.setClass(PDControlActivity.this,
							CtrlModeActivity.class);
					startActivity(intent);
					PDControlActivity.this.finish();
					
				}
				
				/**
				if (client.connectTo(server_ip, server_port) == false) {
					Log.v("login", "connect fail!");
				} else {
					Log.v("login", "connect success!");
					
					Intent intent = new Intent();
					intent.setClass(PDControlActivity.this,
							CtrlModeActivity.class);
					startActivity(intent);
					PDControlActivity.this.finish();
					
				}
				Intent intent = new Intent();
				intent.setClass(PDControlActivity.this,
						CtrlModeActivity.class);
				startActivity(intent);
				PDControlActivity.this.finish();
				**/
			}
		});

	}

}